package educrypt.ciphers.hill;

import java.util.ArrayList;
import java.util.List;

import educrypt.commons.Alphabet;

public class HillNgrams {

	private List<HillNgram> ngrams;

	public HillNgrams(String data, Alphabet alphabet, int ngramSize) {
		generateNgrams(data, alphabet, ngramSize);
	}

	private void generateNgrams(String data, Alphabet alphabet, int ngramSize) {
		this.ngrams = new ArrayList<HillNgram>();
		HillNgram ngram = new HillNgram(alphabet);
		for (int index = 0; index < data.length(); index++) {
			char symbol = data.charAt(index);
			ngram.addSymbol(symbol);

			if (ngram.size() == ngramSize) {
				ngrams.add(ngram);
				ngram = new HillNgram(alphabet);
			} else if (index == data.length() - 1) {
				do {
					char alphabetLastSymbol = alphabet.getSymbolAt(alphabet
							.size() - 1);
					ngram.addSymbol(alphabetLastSymbol);
				} while (ngram.size() < ngramSize);
				ngrams.add(ngram);
				ngram = new HillNgram(alphabet);
			}

		}
	}

	public HillNgram get(int index) {
		return this.ngrams.get(index);
	}

	public int size() {
		return this.ngrams.size();
	}

}
